//
//  LGNetWorKing_Extention.swift
//  微博框架
//
//  Created by ming on 16/10/10.
//  Copyright © 2016年 ming. All rights reserved.
//

import Foundation

extension LGNetWorKingManager {

    /// 获取微博好友微博数据
    ///

    /// 获取微博好友微博数据
    ///
    /// - parameter since_id: 若指定此参数，则返回ID比since_id大的微博（即比since_id时间晚的微博），默认为0。
    /// - parameter max_id:     若指定此参数，则返回ID小于或等于max_id的微博，默认为0。

    /// - parameter completion: 获取结果数据回调
    func statusList(since_id:Int64 = 0, max_id:Int64 = 0, completion:@escaping (_ json:[[String: AnyObject]], _ isSuccess:Bool)->()){
        //从网络中读取数据
        let max = max_id < 1 ? 0 : (max_id - 1)
        let parameters = ["since_id":"\(since_id)","max_id":"\(max)"]
        
     
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        ///token的请求
            tokenRequest(urlString: url, parameters: parameters) { (json, isSuccess) in
            // 提示：服务器返回的字典数组，就是按照时间的倒序排序的
            guard  let dict = json as AnyObject?,
                let status = dict["statuses"] as? [[String: AnyObject]] else{
                    
                    return
            }
            //请求结果成功回调
            completion(status, isSuccess)
            

        }
    
   }      
        
    
    func unreadCount(complition:@escaping (_ count:Int)->()){
        
     let url = "https://rm.api.weibo.com/2/remind/unread_count.json"
        let parameters = ["uid":"1945146207"]
        
        tokenRequest(urlString: url, parameters: parameters) { (json, isSuccess) in
            
            
      guard let dict = json as AnyObject?,
         let count = dict["status"] as? Int else{
            
                return
            
                   }
    complition(count)
     
       
   
        }
        
}
    
    

}

//获取token
extension LGNetWorKingManager{
    
    func loadAccessTtoken(code:String ,completion:@escaping (_ isSUccess:Bool)->()){
        
   
       let url = "https://api.weibo.com/oauth2/access_token"
        print(code)
        let parameters = ["client_id":appkey,
                          "client_secret":appsecret ,
                          "grant_type":"authorization_code",
                          "code":code,
                          "redirect_uri":redirect_uri]
        
        
//        请求失败
        //    ["client_secret": "37af1178bb1f21ae62d7a566f685a6fb", "grant_type": "authorization_code", "code": "69e7503c5caaf3bf94ff230dac222bb4", "client_id": "800817303", "redirect_uri": "http://52it.me"]

        //获取用户数据
        request(method: .POST, urlString: url, parameters: parameters) { (json, isSuccess) in
            
            //拿到用户授权信息字典专模型
            let dict = (json as AnyObject?) as? [String:AnyObject]
         //把字典赋值给属性
       self.userAccess.yy_modelSet(with: dict!)
         //保存模型信息到json中
            self.userAccess.accessSaveToken()
     
            completion(isSuccess)
        }
    }
    
    
}




